package com.onlyphoto.onlymall.mvp.view.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.onlyphoto.onlymall.MyApplication
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.listeners.NoDoubleClickListener
import com.onlyphoto.onlymall.utils.*
import kotlinx.android.synthetic.main.activity_splash.*
import qiu.niorgai.StatusBarCompat
import java.lang.ref.WeakReference

class SplashActivity : AppCompatActivity() {

    var uri: Uri? = null
    private var time: Long = 0

    val goToNext = 1
    val changeNotice = 2

    // 创建一个Handler
    val mHandler: Handler =MyHandler(this@SplashActivity)

    private fun sendMsg() {
        mHandler.sendEmptyMessageDelayed(goToNext, 1000)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        StatusBarCompat.translucentStatusBar(this, true)
        getUri()
        val mainIntent = intent
        val action = mainIntent.action
        if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN == action) {
            if (!isTaskRoot) {
                finish()
                return
            }
        }
        if (Intent.ACTION_VIEW == action) {
            if (!isTaskRoot) {
                finish()
                return
            }
        }
        if (action == null) {
            if (!isTaskRoot) {
                finish()
                return
            }
        }
        initViews()

        mHandler.sendEmptyMessageDelayed(changeNotice, 10)
    }

    private fun getUri() {
        //对URI 数据分发
        uri = intent.data
        if(uri == null) {
            val jumpUri = intent.getStringExtra("jumpUri")
            if (jumpUri != null) {
                uri = Uri.parse(jumpUri)
            }
            SchemeTool.scheme(uri)
        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        getUri()
    }

    private fun initViews() {
        Constants.authRuntimePermissions = true
    }

    fun showSplashAd() {
        val showLanchImg = SharePreferenceUtil.getInstance(this).getIntData(Constants.IS_SHOW_NOTICE_AD, 0)
        val imageUrl = SharePreferenceUtil.getInstance(this).getData(Constants.NOTICE_IMG_URL)
        if (!MyApplication.app.isFirstIn() && showLanchImg == 1 && !StringUtil.isBlank(imageUrl)) {
            showSplashImage(imageUrl)
        }
        time = 3
        mHandler.sendEmptyMessage(goToNext)
    }

    private fun showSplashImage(imageUrl: String) {
        rl_splashAd.visibility = View.VISIBLE
        splash_img.visibility = View.VISIBLE
        Glide.with(this).load(imageUrl).diskCacheStrategy(DiskCacheStrategy.ALL).into(splash_img)

        splash_img.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(view: View) {
                if (mHandler.hasMessages(goToNext)) {
                    mHandler.removeMessages(goToNext)
                }
                val intent = Intent(this@SplashActivity, WebViewActivity::class.java)
                intent.putExtra("title", "")
                intent.putExtra("url", SharePreferenceUtil.getInstance(this@SplashActivity).getData(Constants.NOTICE_ACTION_URL))
                intent.putExtra("JumpToHome", "1")
                startActivity(intent)
                finish()
            }
        })
        tv_passAd.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(view: View) {
                if (mHandler.hasMessages(goToNext)) {
                    mHandler.removeMessages(goToNext)
                }
                realGoNext()
            }
        })
    }

    fun realGoNext(){
        //暂时没有引导页，代码注释
        /*if (MyApplication.app.isFirstIn()) {
            val mLastUpdateTimeMillis = System.currentTimeMillis()
            SharePreferenceUtil.getInstance(this).setLong(Constants.FIRST_OPEN_APP_TIME, mLastUpdateTimeMillis)
            val intent = Intent(this, GuideActivity::class.java)
            startActivity(intent)
            finish()
        } else {
            val intent = Intent(this, MainActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
            startActivity(intent)
            finish()
        }*/
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
        startActivity(intent)
        finish()
    }

    @SuppressLint("SetTextI18n")
    fun goToNext() {
        if (time != 0L) {
            sendMsg()
            tv_passAd.text = "跳过$time"
            time--
            LogUtil.log("Displayed", time.toString())
        } else {
            realGoNext()
            LogUtil.log("Displayed", "noTime")
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        mHandler.removeCallbacksAndMessages(null)
    }

    private class MyHandler(activity: SplashActivity) : Handler() {
        val mActivity: WeakReference<SplashActivity> = WeakReference(activity)

        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                mActivity.get()?.changeNotice -> mActivity.get()?.showSplashAd()
                mActivity.get()?.goToNext -> mActivity.get()?.goToNext()
            }
        }
    }
}
